/**
 * 提交订单
 * @constructor
 */
function ConfirmOrder() {
    this.proId = pid;
    this.np = p;

    this.ajaxUrl = {
        productDetail: "/product/{0}.do",//detail
        address: "/account/get/address.do",
        confirmOrder: "/order/submitOrder.do"
    }

    this.variable = {
        $orderInfoTemp: $("#orderInfo-template"),
        $orderInfo: $("#orderInfo"),
        $totalPrice: $("#totalPrice"),
        $submitOrderBtn: $("#submitOrderBtn")
    }

    this.parmas = {
        pid: pid,
        num: num,
        addId: 0,
        userId: 0,
        propId: spId,
        totalPrice:p
    }

    this.common = new common()   //通用
}
ConfirmOrder.prototype.init = function () {
    var self = this;
    var user = JSON.parse(localStorage.getItem("user"));
    if (!user) {
        alert("登录超时,请重新登录!");
        location.href = "/shop/app/login/login.jsp";
    } else {
        self.parmas.userId = user.id;

        self.getProduct();
        self.getAddress();
        self.submitOrder();
    }
}
ConfirmOrder.prototype.getProduct = function () {
    var self = this;
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.productDetail.format(self.proId),
        dataType: "json",
        data:{
          accountId:self.parmas.userId
        },
        success: function (data) {
            var $bannerObj = self.common.evalJson(data);
            if (!!$bannerObj) {
                var myTemplate = Handlebars.compile(self.variable.$orderInfoTemp.html());
                self.variable.$orderInfo.html(myTemplate(data));
                self.variable.$totalPrice.text(parseInt(self.np));
                $("#nowPrice").text(self.np);
                $("#num").text(num);
            }
        }
    });
}
ConfirmOrder.prototype.addressShipEvent = function () {
    var self = this;
    $(".addressShip").click(function () {
        self.parmas.addId = parseInt($(this).attr("data-id"));
    });
}
/**
 * 收货地址获取
 */
ConfirmOrder.prototype.getAddress = function () {
    var self = this;
    var url = "/h5/address";
    var user = JSON.parse(localStorage.getItem("user"));

    $.post(url, {
        account: user.account
    }, function (result) {
        var source = $('#address-template').html();
        var template = Handlebars.compile(source);
        var html = template(result);
        $("#address-list").append(html);
        self.addressShipEvent();

    })
}
//
ConfirmOrder.prototype.submitOrder = function () {
    var self = this;
    self.variable.$submitOrderBtn.click(function () {
        if (self.parmas.addId == 0) {
            alert("请选择地址");
            return false;
        }
        $.post(self.ajaxUrl.confirmOrder, self.parmas, function (result) {
            var $bannerObj = self.common.evalJson(result);
            if (!!$bannerObj && $bannerObj.status == "success") {
                location.href = "/shop/app/ecshop/pay-order.jsp?pid=" + self.proId + "&orderNo=" + $bannerObj.orderNo;
            }
        })
    });
}
$(function () {

    new ConfirmOrder().init();

    $('.sub').on('click', function () {
        if ($(this).hasClass('subtraction')) {
            return;
        }
        var num = parseInt($('.number').text());
        num--;
        if (num < 2) {
            $('.sub').addClass('subtraction').removeClass('subtractionabled');
        }
        if (num < 10) {
            $('.plus').addClass('plus-able').removeClass('plus-abled');
        }
        $('.number').text(num);
        var totalPrice = num * price;
        var splitPrice = totalPrice.toString().split('.');
        $('.total-price').text(splitPrice[0]);
        $('.sub-price').text(splitPrice[1] || '00');
    });


    $('.plus').on('click', function () {

        if ($(this).hasClass('plus-abled')) {
            return;
        }
        var num = parseInt($('.number').text());
        num++;
        if (num > 1) {
            $('.sub').addClass('subtractionabled').removeClass('subtraction');
        }
        if (num >= 10) {
            $('.plus').addClass('plus-abled').removeClass('plus-able');
        }
        $('.number').text(num);
        var totalPrice = num * price;
        var splitPrice = totalPrice.toString().split('.');
        $('.total-price').text(splitPrice[0]);
        $('.sub-price').text(splitPrice[1] || '00');
    });
})